#pragma once

#include "GIFSource.h"
#include <d2d1.h>
#include <wincodec.h>
#include <d2d1_1.h>
class GifImage
{
public:
	GifImage(void);
	~GifImage(void);
	HRESULT OpenImageFile(LPCWSTR pwszURL);
	HRESULT OpenImageStream(IStream *istream);
	long GetHeight(){return m_Height;}
	long GetWidth(){return m_Width;}
	long GetPixelAspectRatio(){return m_AspectRatio;}
	HRESULT GetGlobalMetadata();
	HRESULT GetFrameBuffByIndex(long index, byte **buff);
	UINT64 GetDuration();
	long GetBuffSize();
	long GetFrameNumbers(){return m_cFrames;}
	UINT GetFrameDelay(){return m_uFrameDelay;}
	HRESULT GifImage::GetBackgroundColor(IWICMetadataQueryReader *pMetadataQueryReader);

private:
	long m_Height;
	long m_Width;
	long m_AspectRatio;

	ID2D1Factory               *m_pD2DFactory;
	ID2D1RenderTarget		   *m_pD2DRenderTarget;
	ID2D1DeviceContext         *m_pD2DDeviceContext;
    ID2D1BitmapRenderTarget    *m_pFrameComposeRT;
	IWICBitmap				   *m_pWICBitmap;
    //ID2D1Bitmap                *m_pRawFrame;
    ID2D1Bitmap1               *m_pSavedFrame;          // The temporary bitmap used for disposal 3 method
    D2D1_COLOR_F                m_backgroundColor;

    IWICImagingFactory         *m_pIWICFactory;
    IWICBitmapDecoder          *m_pDecoder;


	ID2D1DrawingStateBlock		*m_stateBlock;
    UINT    m_uNextFrameIndex;
    UINT    m_uTotalLoopCount;  // The number of loops for which the animation will be played
    UINT    m_uLoopNumber;      // The current animation loop number (e.g. 1 when the animation is first played)
    BOOL    m_fHasLoop;         // Whether the gif has a loop
    UINT    m_cFrames;
    UINT    m_uFrameDisposal;
    UINT    m_uFrameDelay;
    UINT    m_cxGifImage;
    UINT    m_cyGifImage;
    UINT    m_cxGifImagePixel;  // Width of the displayed image in pixel calculated using pixel aspect ratio
    UINT    m_cyGifImagePixel;  // Height of the displayed image in pixel calculated using pixel aspect ratio
    D2D1_RECT_F m_framePosition;

	enum DISPOSAL_METHODS
    {
        DM_UNDEFINED  = 0,
        DM_NONE       = 1,
        DM_BACKGROUND = 2,
        DM_PREVIOUS   = 3 
    };
};

